home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu250.dms / pu250.adf / Graphics / Sprites / Example2.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  12KB  |  411 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: Sprites                     Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program shows how to declare and initialize some sprite data   */
  21. /* and a SimpleSprite structure. It also shows how to reserve a sprite */
  22. /* (sprite 2), and how to move it around. The user moves the sprite by */
  23. /* pressing the arrow keys. In this example we animate the sprite (6   */
  24. /* frames taken from Miniblast).                                       */
  25.  
  26.  
  27.  
  28. #include <intuition/intuition.h>
  29. /* Include this file since you are using sprites: */
  30. #include <graphics/sprite.h>
  31.  
  32.  
  33.  
  34. /* Declare the functions we are going to use: */
  35. void main();
  36. void free_memory();
  37.  
  38.  
  39.  
  40. struct IntuitionBase *IntuitionBase = NULL;
  41. /* We need to open the Graphics library since we are using sprites: */
  42. struct GfxBase *GfxBase = NULL;
  43.  
  44.  
  45.  
  46. /* Declare a pointer to a Window structure: */ 
  47. struct Window *my_window = NULL;
  48.  
  49. /* Declare and initialize your NewWindow structure: */
  50. struct NewWindow my_new_window=
  51. {
  52.   50,            /* LeftEdge    x position of the window. */
  53.   25,            /* TopEdge     y positio of the window. */
  54.   320,           /* Width       320 pixels wide. */
  55.   100,           /* Height      100 lines high. */
  56.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  57.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  58.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  59.   RAWKEY,        /*             user has selected the Close window gad, */
  60.                  /*             or if the user has pressed a key. */
  61.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  62.   WINDOWCLOSE|   /*             Close Gadget. */
  63.   WINDOWDRAG|    /*             Drag gadget. */
  64.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  65.   WINDOWSIZING|  /*             Sizing Gadget. */
  66.   ACTIVATE,      /*             The window should be Active when opened. */
  67.   NULL,          /* FirstGadget No Custom gadgets. */
  68.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  69.   "MICROBLAST",  /* Title       Title of the window. */
  70.   NULL,          /* Screen      Connected to the Workbench Screen. */
  71.   NULL,          /* BitMap      No Custom BitMap. */
  72.   80,            /* MinWidth    We will not allow the window to become */
  73.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  74.   300,           /* MaxWidth    than 300 x 200. */
  75.   200,           /* MaxHeight */
  76.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  77. };
  78.  
  79.  
  80.  
  81. /*********************************************************************/
  82. /* Extra information:                                                */
  83. /* When we declare the window pointer, the intuition library pointer */
  84. /* etc, we initialize them to point to NULL:                         */
  85. /* struct Window *my_window = NULL;                                  */
  86. /* Since we then know that all of the pointers will point to NULL    */
  87. /* when we start, we can check if they still point to NULL when we   */
  88. /* quit. If they do not point to NULL anymore, we close that window, */
  89. /* library etc.                                                      */
  90. /*********************************************************************/
  91.  
  92.  
  93.  
  94. /********************************************************/
  95. /* 1. Declare and initialize some sprite graphics data: */
  96. /********************************************************/
  97. /* Sprite data for a ship: */
  98. /* (6 frames, 4 different images: 1 2 3 4 3 2) */
  99. UWORD chip ship_data[6][28]=
  100. {
  101.   {
  102.       0x0000, 0x0000, /* Ship 1 */
  103.  
  104.       0xFFF8, 0x0000,
  105.       0x0200, 0x0000,
  106.       0x877C, 0x0000,
  107.       0x8786, 0x027C,
  108.       0xBFBF, 0x02C6,
  109.       0xEDFF, 0x1AC2,
  110.       0xA57D, 0x1AFE,
  111.       0xBF19, 0x02FE,
  112.       0x8F12, 0x00FC,
  113.       0x04FC, 0x0000,
  114.       0x0809, 0x0000,
  115.       0x3FFE, 0x0000,
  116.  
  117.       0x0000, 0x0000,
  118.   },
  119.   {
  120.       0x0000, 0x0000, /* Ship 2 */
  121.  
  122.       0x7FF0, 0x0000,
  123.       0x0200, 0x0000,
  124.       0x077C, 0x0000,
  125.       0x8786, 0x027C,
  126.       0xBFBF, 0x02C6,
  127.       0xEDFF, 0x1AC2,
  128.       0xA57D, 0x1AFE,
  129.       0xBF19, 0x02FE,
  130.       0x0F12, 0x00FC,
  131.       0x04FC, 0x0000,
  132.       0x0809, 0x0000,
  133.       0x3FFE, 0x0000,
  134.  
  135.       0x0000, 0x0000,
  136.   },
  137.   {
  138.       0x0000, 0x0000, /* Ship 3 */
  139.  
  140.       0x3FE0, 0x0000,
  141.       0x0200, 0x0000,
  142.       0x877C, 0x0000,
  143.       0x8786, 0x027C,
  144.       0xBFBF, 0x02C6,
  145.       0xEDFF, 0x1AC2,
  146.       0xA57D, 0x1AFE,
  147.       0xBF19, 0x02FE,
  148.       0x8F12, 0x00FC,
  149.       0x04FC, 0x0000,
  150.       0x0809, 0x0000,
  151.       0x3FFE, 0x0000,
  152.  
  153.       0x0000, 0x0000,
  154.   },
  155.   {
  156.       0x0000, 0x0000, /* Ship 4 */
  157.  
  158.       0x1FC0, 0x0000,
  159.       0x0200, 0x0000,
  160.       0x077C, 0x0000,
  161.       0x8786, 0x027C,
  162.       0xBFBF, 0x02C6,
  163.       0xEDFF, 0x1AC2,
  164.       0xA57D, 0x1AFE,
  165.       0xBF19, 0x02FE,
  166.       0x0F12, 0x00FC,
  167.       0x04FC, 0x0000,
  168.       0x0809, 0x0000,
  169.       0x3FFE, 0x0000,
  170.  
  171.       0x0000, 0x0000,
  172.   },
  173.   {
  174.       0x0000, 0x0000, /* Ship 5 (3) */
  175.  
  176.       0x3FE0, 0x0000,
  177.       0x0200, 0x0000,
  178.       0x877C, 0x0000,
  179.       0x8786, 0x027C,
  180.       0xBFBF, 0x02C6,
  181.       0xEDFF, 0x1AC2,
  182.       0xA57D, 0x1AFE,
  183.       0xBF19, 0x02FE,
  184.       0x8F12, 0x00FC,
  185.       0x04FC, 0x0000,
  186.       0x0809, 0x0000,
  187.       0x3FFE, 0x0000,
  188.  
  189.       0x0000, 0x0000,
  190.   },
  191.   {
  192.       0x0000, 0x0000, /* Ship 6 (2) */
  193.  
  194.       0x7FF0, 0x0000,
  195.       0x0200, 0x0000,
  196.       0x077C, 0x0000,
  197.       0x8786, 0x027C,
  198.       0xBFBF, 0x02C6,
  199.       0xEDFF, 0x1AC2,
  200.       0xA57D, 0x1AFE,
  201.       0xBF19, 0x02FE,
  202.       0x0F12, 0x00FC,
  203.       0x04FC, 0x0000,
  204.       0x0809, 0x0000,
  205.       0x3FFE, 0x0000,
  206.  
  207.       0x0000, 0x0000,
  208.   }
  209. };
  210.  
  211.  
  212.  
  213. /*******************************************************/
  214. /* 2. Declare and initialize a SimpleSprite structure: */
  215. /*******************************************************/
  216. struct SimpleSprite my_sprite=
  217. {
  218.   ship_data[0],   /* posctldata, pointer to the sprite data. (Frame 0) */
  219.   12,             /* height, 12 lines tall. */
  220.   40, 80,         /* x, y, position on the screen. */
  221.   -1,             /* num, this field is automatically initialized when  */
  222.                   /* you call the GetSprite() function, so we set it to */
  223.                   /* -1 for the moment.                                 */
  224. };
  225.  
  226.  
  227.  
  228. void main()
  229. {
  230.   /* Sprite position: */
  231.   WORD x = my_sprite.x;
  232.   WORD y = my_sprite.y;
  233.  
  234.   /* Direction of the sprite: */
  235.   WORD x_direction = 0;
  236.   WORD y_direction = 0;
  237.  
  238.   UWORD frame = 0; /* Frame 0 */
  239.  
  240.   /* Boolean variable used for the while loop: */
  241.   BOOL close_me = FALSE;
  242.  
  243.   ULONG class; /* IDCMP */
  244.   USHORT code; /* Code */
  245.  
  246.   /* Declare a pointer to an IntuiMessage structure: */
  247.   struct IntuiMessage *my_message;
  248.  
  249.  
  250.  
  251.   /* Open the Intuition Library: */
  252.   IntuitionBase = (struct IntuitionBase *)
  253.     OpenLibrary( "intuition.library", 0 );
  254.   
  255.   if( IntuitionBase == NULL )
  256.     free_memory(); /* Could NOT open the Intuition Library! */
  257.  
  258.  
  259.  
  260.   /* Since we are using sprites we need to open the Graphics Library: */
  261.   /* Open the Graphics Library: */
  262.   GfxBase = (struct GfxBase *)
  263.     OpenLibrary( "graphics.library", 0);
  264.  
  265.   if( GfxBase == NULL )
  266.     free_memory(); /* Could NOT open the Graphics Library! */
  267.  
  268.  
  269.  
  270.   /* We will now try to open the window: */
  271.   my_window = (struct Window *) OpenWindow( &my_new_window );
  272.   
  273.   /* Have we opened the window succesfully? */
  274.   if(my_window == NULL)
  275.     free_memory(); /* Could NOT open the Window! */
  276.  
  277.  
  278.  
  279.   /* Change the colour register 21 - 23: */
  280.   SetRGB4( &my_window->WScreen->ViewPort, 21, 0x0, 0x0, 0x0 ); /* Black */
  281.   SetRGB4( &my_window->WScreen->ViewPort, 22, 0x0, 0x8, 0x0 ); /* DGreen */
  282.   SetRGB4( &my_window->WScreen->ViewPort, 23, 0x0, 0xD, 0x0 ); /* Green */
  283.  
  284.  
  285.  
  286.   /*******************************/
  287.   /* 3. Try to reserve sprite 2: */
  288.   /*******************************/
  289.   if( GetSprite( &my_sprite, 2 ) != 2 )
  290.     free_memory(); /* Could not reserve sprite number 2. */
  291.  
  292.  
  293.  
  294.   /* Stay in the while loop until the user has selected the Close window */
  295.   /* gadget: */
  296.   while( close_me == FALSE )
  297.   {
  298.     /* Stay in the while loop as long as we can collect messages */
  299.     /* sucessfully: */
  300.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  301.     {
  302.       /* After we have collected the message we can read it, and save any */
  303.       /* important values which we maybe want to check later: */
  304.       class = my_message->Class;
  305.       code  = my_message->Code;
  306.  
  307.  
  308.       /* After we have read it we reply as fast as possible: */
  309.       /* REMEMBER! Do never try to read a message after you have replied! */
  310.       /* Some other process has maybe changed it. */
  311.       ReplyMsg( my_message );
  312.  
  313.  
  314.       /* Check which IDCMP flag was sent: */
  315.       switch( class )
  316.       {
  317.         case CLOSEWINDOW:     /* Quit! */
  318.                close_me=TRUE;
  319.                break;  
  320.  
  321.         case RAWKEY:          /* A key was pressed! */
  322.                /* Check which key was pressed: */
  323.                switch( code )
  324.                {
  325.                  /* Up Arrow: */
  326.                  case 0x4C:      y_direction = -1; break; /* Pressed */
  327.                  case 0x4C+0x80: y_direction = 0;  break; /* Released */
  328.  
  329.                  /* Down Arrow: */
  330.                  case 0x4D:      y_direction = 1; break; /* Pressed */
  331.                  case 0x4D+0x80: y_direction = 0; break; /* Released */
  332.  
  333.                  /* Right Arrow: */
  334.                  case 0x4E:      x_direction = 1; break; /* Pressed */
  335.                  case 0x4E+0x80: x_direction = 0; break; /* Released */
  336.  
  337.                  /* Left Arrow: */
  338.                  case 0x4F:      x_direction = -1; break; /* Pressed */
  339.                  case 0x4F+0x80: x_direction = 0;  break; /* Released */
  340.                }
  341.                break;  
  342.       }
  343.     }
  344.  
  345.  
  346.  
  347.     /* Change the x/y position: */
  348.     x += x_direction;
  349.     y += y_direction;
  350.     
  351.     /* Check that the sprite does not move outside the screen: */
  352.     if(x > 320)
  353.       x = 320;
  354.     if(x < 0)
  355.       x = 0;
  356.     if(y > 200)
  357.       y = 200;
  358.     if(y < 0)
  359.       y = 0;
  360.  
  361.     /* Move the sprite: */
  362.     MoveSprite( 0, &my_sprite, x, y );
  363.  
  364.  
  365.  
  366.     /* Change frame: */
  367.     frame++;
  368.       
  369.     /* 6 frames: */
  370.     if( frame > 5 )
  371.       frame = 0;
  372.       
  373.     /* Change the sprite data: */
  374.     ChangeSprite( 0, &my_sprite, ship_data[ frame ] );
  375.  
  376.  
  377.  
  378.     /* Wait for the videobeam to reach the top of the display: (This */
  379.     /* will slow down the animation so the user can see the sprite)  */
  380.     /* (If you want to have some "action" you can take it away...) */
  381.     WaitTOF();
  382.   }
  383.  
  384.  
  385.  
  386.   /* Free all allocated memory: (Close the window, libraries etc) */
  387.   free_memory();
  388.  
  389.   /* THE END */
  390. }
  391.  
  392.  
  393.  
  394. /* This function frees all allocated memory. */
  395. void free_memory()
  396. {
  397.   if( my_sprite.num != -1 )
  398.     FreeSprite( my_sprite.num );
  399.  
  400.   if( my_window )
  401.     CloseWindow( my_window );
  402.   
  403.   if( GfxBase )
  404.     CloseLibrary( GfxBase );
  405.  
  406.   if( IntuitionBase )
  407.     CloseLibrary( IntuitionBase );
  408.  
  409.   exit();
  410. }
  411.